package com.tlgen.controller.studentAffair;

import com.tlgen.model.Assist;
import com.tlgen.model.RespBean;
import com.tlgen.model.RespPageBean;
import com.tlgen.service.AssistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: tlgen
 * @Discription:
 */
@RestController
@RequestMapping("/studentaffair/assist")
public class AssistController {

    @Autowired
    AssistService assistService;
/*

    @GetMapping("/")
    public List<Assist> getAllAssists() {
        return assistService.getAllAssists();
    }
*/

    @PostMapping("/")
    public RespBean addAssist(@RequestBody Assist assist) {
        if (assistService.addAssist(assist) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");
    }
/*

    @PutMapping("/")
    public RespBean updateAssist(@RequestBody Assist assist) {
        if (assistService.updateAssist(assist) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }
*/

    @DeleteMapping("/{id}")
    public RespBean deleteAssistById(@PathVariable Integer id) {
        if (assistService.deleteAssistById(id) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @DeleteMapping("/")
    public RespBean deleteAssistByIds(Integer[] ids) {
        if (assistService.deleteAssistByIds(ids) == ids.length) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }


    @GetMapping("/")
    public RespPageBean getAssistByPageWithSalary(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        return assistService.getAssistByPageWithSalary(page, size);
    }

    @PutMapping("/")
    public RespBean updateAssistSalaryById(Integer aid, Integer sid) {
        Integer result = assistService.updateAssistSalaryById(aid, sid);
        /*
         * 因为使用replace语句 不管是result==1 或者 result ==2 都是代表成功的状态 replace会执行两次
         * */
        if (result == 1 || result == 2) {
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
}
